package com.crm.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.crm.pojo.Linkman;
import com.crm.pojo.PageInfo;
import com.crm.service.LinkmanService;
import com.crm.service.impl.LinkmanServiceImpl;

public class LinkmanServlet extends HttpServlet {
	LinkmanService linkmanService = new LinkmanServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String flag = req.getParameter("flag");
		//进入新增页面
		if(flag.equals("goadd")) {
			goadd(req,resp);
		}
		//实现新增
		if(flag.equals("add")) {
			add(req,resp);
		}
		//列表
		if(flag.equals("query")) {
			query(req,resp);
		}
		//删除
		if(flag.equals("del")) {
			delete(req,resp);
		}
		//进入修改
		if(flag.equals("goedit")) {
			goedit(req,resp);
		}
		//修改
		if(flag.equals("edit")) {
			edit(req,resp);
		}
	}
	
	//提交修改
	private void edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String lkmName = req.getParameter("lkmName");
		if(lkmName!=null && !lkmName.equals("")) {
			Integer lkmId = Integer.parseInt(req.getParameter("lkmId"));
			Integer lkmCustId = Integer.parseInt(req.getParameter("lkmCustId"));
			String lkmGender = req.getParameter("lkmGender");
			String lkmPhone = req.getParameter("lkmPhone");
			String lkmMobile = req.getParameter("lkmMobile");
			String lkmEmail = req.getParameter("lkmEmail");
			String lkmQQ = req.getParameter("lkmQQ");
			String lkmPosition = req.getParameter("lkmPosition");
			String lkmMemo = req.getParameter("lkmMemo");
			Linkman linkman = new Linkman();
			linkman.setLkmId(lkmId);
			linkman.setLkmName(lkmName);
			linkman.setLkmCustId(lkmCustId);
			linkman.setLkmGender(lkmGender);
			linkman.setLkmPhone(lkmPhone);
			linkman.setLkmMobile(lkmMobile);
			linkman.setLkmEmail(lkmEmail);
			linkman.setLkmQQ(lkmQQ);
			linkman.setLkmPosition(lkmPosition);
			linkman.setLkmMemo(lkmMemo);
			boolean flag =  linkmanService.updateLinkman(linkman);
			if(flag) {
				resp.sendRedirect(req.getContextPath()+"/success.jsp");
			}else {
				resp.sendRedirect(req.getContextPath()+"/error.jsp");
			}
		}else {
			resp.sendRedirect(req.getContextPath()+"/error.jsp");
		}
	}
	
	//进入修改
	private void goedit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Integer lkmId = Integer.parseInt(req.getParameter("lkmId"));
		Map linkman = linkmanService.queryLinkman(lkmId);
		List<Map> customers = linkmanService.queryCustomer();
		req.setAttribute("customers", customers);
		req.setAttribute("linkman", linkman);
		req.getRequestDispatcher("linkman/edit.jsp").forward(req, resp);
	}

	//删除
	private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Integer lkmId = Integer.parseInt(req.getParameter("lkmId")); 
		boolean flag = linkmanService.deleteLinkman(lkmId);
		if(flag) {
			resp.sendRedirect(req.getContextPath()+"/success.jsp");
		}else {
			resp.sendRedirect(req.getContextPath()+"/error.jsp");
		}
	}

	//列表
	private void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String lkmName = req.getParameter("lkmName");
		if(lkmName==null) {
			lkmName = "";
		}
		Integer pageSize;
		if(req.getParameter("pageSize")!=null) {
			pageSize = Integer.parseInt(req.getParameter("pageSize"));
		}else {
			pageSize = 15;
		}
		Integer pageNumber;
		if(req.getParameter("pageNumber")!=null) {
			pageNumber = Integer.parseInt(req.getParameter("pageNumber"));
		}else {
			pageNumber = 1;
		}
		PageInfo pageLinkman = linkmanService.queryLinkman(lkmName,pageSize,pageNumber);
		req.setAttribute("lkmName", lkmName);
		req.setAttribute("pageLinkman", pageLinkman);
		req.getRequestDispatcher("linkman/list.jsp").forward(req, resp);
	}

	//实现新增
	private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String lkmName = req.getParameter("lkmName");
		if(lkmName!=null && !lkmName.equals("")) {
			Integer lkmCustId = Integer.parseInt(req.getParameter("lkmCustId"));
			String lkmGender = req.getParameter("lkmGender");
			String lkmPhone = req.getParameter("lkmPhone");
			String lkmMobile = req.getParameter("lkmMobile");
			String lkmEmail = req.getParameter("lkmEmail");
			String lkmQQ = req.getParameter("lkmQQ");
			String lkmPosition = req.getParameter("lkmPosition");
			String lkmMemo = req.getParameter("lkmMemo");
			Linkman linkman = new Linkman();
			linkman.setLkmName(lkmName);
			linkman.setLkmCustId(lkmCustId);
			linkman.setLkmGender(lkmGender);
			linkman.setLkmPhone(lkmPhone);
			linkman.setLkmMobile(lkmMobile);
			linkman.setLkmEmail(lkmEmail);
			linkman.setLkmQQ(lkmQQ);
			linkman.setLkmPosition(lkmPosition);
			linkman.setLkmMemo(lkmMemo);
			boolean flag =  linkmanService.insertLinkman(linkman);
			if(flag) {
				resp.sendRedirect(req.getContextPath()+"/success.jsp");
			}else {
				resp.sendRedirect(req.getContextPath()+"/error.jsp");
			}
		}else {
			resp.sendRedirect(req.getContextPath()+"/error.jsp");
		}
		
	}

	//进入新增页面
	private void goadd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//获取所有客户名称
		List<Map> customers = linkmanService.queryCustomer();
		req.setAttribute("customers", customers);
		//转发到linkman/add.jsp
		req.getRequestDispatcher("linkman/add.jsp").forward(req, resp);
	}

}
